package kd.bos.kdec.pincar;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kd.bos.bill.MobileBillShowParameter;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.filter.FilterParameter;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.list.events.ListRowClickEvent;
import kd.bos.list.events.ListRowClickListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;

/**
 * 拼车司机端订单列表
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobOrderList extends AbstractFormPlugin implements ListRowClickListener {

	private String[] buttons = new String[] { "entity", "billlistdr" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
		BillList billList = this.getControl("billlistap");
		billList.addListRowClickListener(this);
	}

	@Override
	public void listRowClick(ListRowClickEvent evt) {
		ListRowClickListener.super.listRowClick(evt);
		// 详情跳转
		BillList billList = this.getControl("billlistap");
		ListSelectedRow listSelectedRow = billList.getCurrentSelectedRowInfo();
		Long pk = (Long) listSelectedRow.getPrimaryKeyValue();
		MobileBillShowParameter showParameter = new MobileBillShowParameter();
		showParameter.setFormId("kdec_pincarordermob");
		showParameter.setPkId(pk);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		this.getView().showForm(showParameter);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		if (StringUtils.equals("entity", key)) {// 详情
			BillList billList = this.getControl("billlistap");
			ListSelectedRow listSelectedRow = billList.getCurrentSelectedRowInfo();
			Long pk = (Long) listSelectedRow.getPrimaryKeyValue();
			MobileBillShowParameter showParameter = new MobileBillShowParameter();
			showParameter.setFormId("kdec_pincarordermob");
			showParameter.setPkId(pk);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		} else if (StringUtils.equals("billlistdr", key)) {// 行程单（司机首页跳转）
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_pincar_driver");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		IFormView view = this.getView();
		BillList billList = this.getControl("billlistap");
		int count = billList.getListModel().getBillDataCount();
		if (count == 0) {
			view.setVisible(true, "flexpanelap");
			view.setVisible(false, "billlistap");
		}
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		// 加载单据列表添加筛选条件(已取消)
		BillList billList = this.getControl("billlistap");
		FilterParameter filterParameter = new FilterParameter();
		long userId = Long.parseLong(RequestContext.get().getUserId());
		QFilter filter = new QFilter("orderstatus", QCP.not_equals, "9");
		// 登录用户隔离订单
		QFilter filter1 = new QFilter("creator", QCP.equals, userId);
		List<QFilter> qFilters = new ArrayList<>();
		qFilters.add(filter);
		qFilters.add(filter1);
		filterParameter.setQFilters(qFilters);
		billList.setFilterParameter(filterParameter);
	}
}
